::: scene subject

# GORM 框架篇

## 查询单条记录的方法

:::

::: scene code First 方法

```go
db.First(&user)
```

L1->GORM 框架提供了 First 方法去查询数据表的第一条记录

```sql
SELECT * FROM `users` ORDER BY `users`.`id` LIMIT 1
```

<Sql data={[{sql: "SELECT * FROM users",records: [{ id: 1, name: "shann" },{ id: 2, name: "Linda" },],},{sql: "SELECT * FROM users ORDER BY users.id LIMIT 1",records: [{ id: 1, name: "shann" }],},]}/>


:::

::: scene code Take 方法

```go
db.Take(&user)
```

L1->GORM 框架提供了 Take 方法去查询数据表的第一条记录，它跟 First 的区别是不按字段排序。

```sql
SELECT * FROM users LIMIT 1;
```

<Sql data={[{sql: "SELECT * FROM users",records: [{ id: 1, name: "shann" },{ id: 2, name: "Linda" },],},{sql: "SELECT * FROM users LIMIT 1",records: [{ id: 1, name: "shann" }],},]}/>

:::

::: scene code Last 方法

```go
db.Last(&user)
```

L1->GORM 框架提供了 Take 方法去查询数据表的最后一条记录。

```sql
SELECT * FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` DESC LIMIT 1
```

<Sql data={[{sql: "SELECT * FROM users",records: [{ id: 1, name: "shann" },{ id: 2, name: "Linda" },],},{sql: "SELECT * FROM users WHERE users.id = 1 ORDER BY users.id DESC LIMIT 1",records: [{ id: 2, name: "Linda" }],},]}/>

:::

::: scene link 更多资料 https://imshann.gitee.io/study-go/gorm/query.html

:::

::: scene 感谢观看

<p className="text-center text-4xl mt-32">如果你喜欢我创作的内容，邀请你关注、投币或收藏</p>
<p className="text-center text-4xl mt-24" style={{ color: "#ffde00" }}>Author：黑爷</p>

:::

